package com.htfyun.serialport.uartservo.AX12_ServoUpdate;

import com.htfyun.serialport.uartservo.IServoCommand;
import com.htfyun.serialport.utils.SerialDebugLog;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class AX12Update_ServoCommand extends IServoCommand {

    private static final int CMD_LENGTH_SIZE = 2;
    @Override
    public byte[] getHeader() {
        return AX12Update_ReceivedSerialPacket.getHeader();
    }

    @Override
    public byte[] getCheckSum() {
        byte[] id = getId();
        byte[] parameters = getParameters();
        byte[] instruction = getInstruction();

        byte[] retByte = new byte[0];
        if (id == null) {
            SerialDebugLog.e(" error id is null");
            return retByte;
        }

        if (parameters == null) {
            parameters = new byte[0];
        }

        if (instruction == null) {
            SerialDebugLog.e(" error instruction is null");
            return retByte;
        }

        short length = (short) (parameters.length + instruction.length + id.length); //指令包的长度，该值表示为“参数个数(N)+ 命令长度+ id 长度”

        int checkSumInt = 0;

        for (int i = 0; i < 2; i++) {
            checkSumInt += (0xff & (length >> 8 * i));
        }

        for (byte b : id) {
            checkSumInt += (int) (b & 0xff);
        }

        for (byte b : instruction) {
            checkSumInt += (int) (b & 0xff);
        }

        for (byte b : parameters) {
            checkSumInt += (int) (b & 0xff);
        }

        checkSumInt = ~checkSumInt;

        return new byte[]{(byte) checkSumInt};
    }

    @Override
    public byte[] buildForCommand() {
        byte[] header = getHeader();
        byte[] id = getId();
        byte[] parameters = getParameters();
        byte[] instruction = getInstruction();
        byte[] checkSum = getCheckSum();

        byte[] retByte = new byte[0];

        if (header == null) {
            SerialDebugLog.e(" error header is null");
            return retByte;
        }

        if (id == null) {
            SerialDebugLog.e(" error id is null");
            return retByte;
        }

        if (parameters == null) {
            parameters = new byte[0];
        }

        if (instruction == null) {
            SerialDebugLog.e(" error instruction is null");
            return retByte;
        }

        if (checkSum == null) {
            SerialDebugLog.e(" error checkSum is null");
            return retByte;
        }

        short length = (short) (parameters.length + instruction.length + id.length); //指令包的长度，该值表示为“参数个数(N)+ 命令长度+ id 长度”

        int commandLen = header.length + id.length + CMD_LENGTH_SIZE/*length*/ + instruction.length + parameters.length + checkSum.length;
        ByteBuffer byteBuffer = ByteBuffer.allocate(commandLen);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);//小端模式
        byteBuffer.rewind();
        byteBuffer.put(header);
        byteBuffer.put(id);
        byteBuffer.putShort(length);
        byteBuffer.put(instruction);
        byteBuffer.put(parameters);
        byteBuffer.put(checkSum);

        return byteBuffer.array();
    }

}
